package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.system.service.IQyiSupplierService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.QyiGoods;
import com.ruoyi.system.service.IQyiGoodsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品管理Controller
 * 
 * @author ruoyi
 * @date 2024-12-20
 */
@Controller
@RequestMapping("/system/goods")
public class QyiGoodsController extends BaseController
{
    private String prefix = "system/goods";

    @Autowired
    private IQyiGoodsService qyiGoodsService;

    @Autowired
    private IQyiSupplierService qyiSupplierService;

    @RequiresPermissions("system:goods:view")
    @GetMapping("")
    public String goods()
    {
        return prefix + "/goods";
    }

    /**
     * 查询商品管理列表
     */
    @ApiOperation("商品列表")
    @RequiresPermissions("system:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(QyiGoods qyiGoods)
    {
        startPage();
        List<QyiGoods> list = qyiGoodsService.selectQyiGoodsList(qyiGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品管理列表
     */
    @RequiresPermissions("system:goods:export")
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(QyiGoods qyiGoods)
    {
        List<QyiGoods> list = qyiGoodsService.selectQyiGoodsList(qyiGoods);
        ExcelUtil<QyiGoods> util = new ExcelUtil<QyiGoods>(QyiGoods.class);
        return util.exportExcel(list, "商品管理数据");
    }

    /**
     * 新增商品管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("suppliers", qyiSupplierService.selectSupplierAll());
        return prefix + "/add";
    }

    /**
     * 新增保存商品管理
     */
    @RequiresPermissions("system:goods:add")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(QyiGoods qyiGoods)
    {
        return toAjax(qyiGoodsService.insertQyiGoods(qyiGoods));
    }

    /**
     * 修改商品管理
     */
    @RequiresPermissions("system:goods:edit")
    @GetMapping("/edit/{goodsId}")
    public String edit(@PathVariable("goodsId") Long goodsId, ModelMap mmap)
    {
        qyiSupplierService.selectQyiSupplierByGoodsId(goodsId);
        QyiGoods qyiGoods = qyiGoodsService.selectQyiGoodsByGoodsId(goodsId);
        mmap.put("qyiGoods", qyiGoods);
        mmap.put("suppliers", qyiSupplierService.selectQyiSupplierByGoodsId(goodsId));
        return prefix + "/edit";
    }

    /**
     * 修改保存商品管理
     */
    @RequiresPermissions("system:goods:edit")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(QyiGoods qyiGoods)
    {
        return toAjax(qyiGoodsService.updateQyiGoods(qyiGoods));
    }

    /**
     * 删除商品管理
     */
    @RequiresPermissions("system:goods:remove")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(qyiGoodsService.deleteQyiGoodsByGoodsIds(ids));
    }
}
